package com.taihua_tech.basic.util;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;



public class MyBatisUtil {
	public static SqlSession  getSession() throws IOException{
		//SqlSessionFactoryBuilder
		SqlSessionFactoryBuilder  builder=new SqlSessionFactoryBuilder();
		
		String conf="SqlMapConfig.xml";
		Reader reader=Resources.getResourceAsReader(conf);

		//SqlSessionFactory
		SqlSessionFactory  factory =  builder.build(reader);
		 

		//SqlSession
		SqlSession session=factory.openSession();
		
		return session;
	}
	
	public static SqlSession  getSession2() throws IOException{
		SqlSessionFactoryBuilder  builder=new SqlSessionFactoryBuilder();
		String conf="SqlMapConfig.xml";
		Reader reader=Resources.getResourceAsReader(conf);
		SqlSessionFactory  factory =  builder.build(reader,"environment2",new Properties());
		factory.openSession(false);
		//SqlSession session=factory.openSession();
		SqlSession session=factory.openSession(true);
		return session;
	}
	
	
	public static void main(String[] args) throws IOException {
		SqlSession session=MyBatisUtil.getSession2();
		System.out.println(session);
		Properties pro=session.getConfiguration().getVariables();
		System.out.println(pro);
		System.out.println(pro.getProperty("driver"));
		System.out.println(pro.getProperty("url"));
		System.out.println(pro.getProperty("name"));
		System.out.println(pro.getProperty("password"));
		session.close();
		
//		SqlSession session2=MyBatisUtil.getSession2();
//		System.out.println(session2);
	}
}
